﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace ChordCreator.ViewServices
{
    class ColorPool
    {
        private Color[] colors = new[] {Color.Red, Color.Blue, Color.Brown, Color.Orange, Color.DimGray};
        private Random randomer_;
        private List<Color> usedColors_ = new List<Color>();

        private int currentColorPosition;
        private bool useRandom_;

        public ColorPool(bool useRandom)
        {
            useRandom_ = useRandom;
            randomer_ = new Random();
        }

        public bool RandomSuspended { get; set; }

        public Color GetColor()
        {
            if (usedColors_.Count != colors.Count() || RandomSuspended)
            {
                if (useRandom_)
                {
                    Color[] freeColors = colors.Where(x => !usedColors_.Contains(x)).ToArray();
                    int nextColor = randomer_.Next(0, freeColors.Count() - 1);
                    usedColors_.Add(freeColors[nextColor]);
                    return freeColors[nextColor];
                }
                else
                {
                    Color color = colors[currentColorPosition];
                    usedColors_.Add(color);
                    currentColorPosition++;
                    return color;
                }
            }

            currentColorPosition++;
            if (usedColors_.Count <= currentColorPosition)
            {
                currentColorPosition = 0;
            }
            return usedColors_[currentColorPosition];

        }
    }
}
